﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindStyle.SPEntity;
using WindStyle.SPEntity.Attributes;
using WindStyle.SPEntity.Converters;

namespace UnitTest
{
    public class CustomFieldEntity : Entity
    {
        [Field("CustomField")]
        public string Value { get; set; }
    }

    [Converter(typeof(CustomFieldConverter))]
    public class CustomField : SPField
    {
        public override Type FieldValueType
        {
            get
            {
                return typeof(CustomFieldValue);
            }
        }

        public CustomField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        { }
    }

    [Serializable]
    public class CustomFieldValue
    {
        public string Value { get; set; }
        public CustomFieldValue()
        { }

        public CustomFieldValue(string value)
        {
            this.Value = value;
        }

        public override string ToString()
        {
            return this.Value;
        }
    }

    public class CustomFieldConverter : IValueConverter
    {
        public object ToFieldValue(object value, Type targetType, ConversionContext context)
        {
            return new CustomFieldValue(value as string);
        }

        public object ToPropertyValue(object value, Type targetType, ConversionContext context)
        {
            if (targetType == typeof(string))
            {
                return (value as CustomFieldValue).Value;
            }
            return null;
        }
    }
}
